﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.Store.Orders
{
    public partial class Form_Goods : FormPage
    {
        BLL.Orders_Goods bll = new BLL.Orders_Goods();
        Model.Orders_Goods model = new Model.Orders_Goods();
        //
        BLL.Orders bllOrders = new BLL.Orders();
        Model.Orders modelOrders = new Model.Orders();
        //
        protected int GoodsID = 0;
        BLL.V_Goods bllV_Goods = new BLL.V_Goods();
        protected Model.V_Goods modelV_Goods = new Model.V_Goods();
        Model.Store CurrentStore = new Model.Store();
        protected void Page_Load(object sender, EventArgs e) { 
            DataTypePlus.getPageRequest(Request.QueryString["GoodsID"], ref GoodsID);
            try
            {
                modelV_Goods = bllV_Goods.GetModelList("id=" + GoodsID)[0];
            }
            catch 
            {
            }
            
            CurrentStore = Security.GetCurrentStore();
            //获得当前Orders
            //List<Model.Orders> listModelOrders_WaitMoney = bllOrders.GetModelList("StoreID=" + CurrentStore .id+ " and State<=" + (int)EnumHelper.OrdersState.WaitMoney);
            //if (listModelOrders_WaitMoney.Count == 0)
            //{
            //    modelOrders.StoreID = CurrentStore.id;
            //    modelOrders.Number = DateTime.Now.Year + "" + DateTime.Now.Month.ToString().PadLeft(2, '0') + "" + DateTime.Now.Day.ToString().PadLeft(2, '0') + CurrentStore.Number;
            //    modelOrders.State = (int)EnumHelper.OrdersState.NoGoods;
            //    int OrdersID_new = bllOrders.Add(modelOrders);
            //    modelOrders = bllOrders.GetModel(OrdersID_new);
            //}
            //else modelOrders = listModelOrders_WaitMoney[0];
            modelOrders = BLL.Orders.GetModelOrders_Now(CurrentStore);
            //获得可能存在的Master.id
            List<Model.Orders_Goods> listModelOrders_Goods = bll.GetModelList("OrdersID=" + modelOrders.id + " and GoodsID=" + GoodsID);
            if (listModelOrders_Goods.Count > 0) Master.id = listModelOrders_Goods[0].id;
        }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel()
        {
            model = bll.GetModel(Master.id);
        }

        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            rp_Discount.DataSource = BLL.Goods.GetListDiscountSell_ing(GoodsID);
            rp_Discount.DataBind();
            if (rp_Discount.Items.Count == 0) group_Discount.Visible = false;

            rp_GiftNumber.DataSource = BLL.Goods.GetListGiftNumberSell_ing(GoodsID);
            rp_GiftNumber.DataBind();
            if (rp_GiftNumber.Items.Count == 0) group_GiftNumber.Visible = false;

            hdnnormnum.Value = modelV_Goods.Normnum.ToString();
            if (model.id <1)
            {
                tb_ExpectAmount.Text = modelV_Goods.Normnum.ToString();
            }
            //lblnorm.Text = modelV_Goods.Norm;
        }

        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            tb_ExpectAmount.Text = model.ExpectAmount.ToString();
            tb_Description.Text = model.Description;

        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            int ExpectAmount = DataTypePlus.requestInt(tb_ExpectAmount.Text);
            if (Master.noZero(ExpectAmount, "至少需要订购1个"))
            {
                int old_ExpectAmount = model.ExpectAmount;
                model.OrdersID = modelOrders.id;
                model.GoodsID = GoodsID;
                model.ExpectAmount = ExpectAmount;
                model.Description =tb_Description.Text;
                if (Master.formState == EnumHelper.FormState.add)
                {
                    model.WaitSupplierCheckNum = ExpectAmount;
                    Master.id = bll.Add(model);
                }
                else
                {
                    model.WaitSupplierCheckNum += ExpectAmount - old_ExpectAmount;
                    if (model.WaitSupplierCheckNum < 0) model.WaitSupplierCheckNum = 0;
                    bll.Update(model);
                }
                //
                modelOrders.State = (int)EnumHelper.OrdersState.WaitMoney;
                bllOrders.Update(modelOrders);
                //
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Goods_BuyPrice_Log, Master.id, Master.formState == EnumHelper.FormState.add ? EnumHelper.OperateType.Add : EnumHelper.OperateType.Update, "");
                //

                Master.SubmitOK();
            }
            //string Name = PageValidate.SqlTextClear(DataTypePlus.requestString(tb_Name.Text));

            //if (
            //    Master.noErr(Name == "", "[品牌名称]必须填写") &&
            //    //PageValidate.noEmpty(Name, "[品牌名称]必须填写") &&
            //    Master.noChongfu("Name='" + Name + "'", "[品牌名称]重复")
            //    )
            //{
            //    //图片
            //    string Pic = DataTypePlus.requestString(tb_Pic.Text);
            //    if (model.PicID > 0 && modelPic.URL != Pic) bllPic.Delete(modelPic.id);//删除老图
            //    if (Pic == "") model.PicID = 0;
            //    else if (modelPic.URL != Pic)
            //    {
            //        modelPic.URL = Pic;
            //        System.Drawing.Image bmp = System.Drawing.Image.FromFile(this.Server.MapPath(Server.UrlDecode(Pic)));
            //        modelPic.Width = bmp.Width;
            //        modelPic.Height = bmp.Height;
            //        model.PicID = bllPic.Add(modelPic);
            //    }
            //    //
            //    model.Name = Name;
            //    if (Master.formState == EnumHelper.FormState.add) Master.id = bll.Add(model);
            //    else bll.Update(model);
            //    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Goods_BuyPrice_Log, Master.id, Master.formState == EnumHelper.FormState.add ? EnumHelper.OperateType.Add : EnumHelper.OperateType.Update, "");
            //    Master.SubmitOK();

            //}
        }
    }
}